﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

using Newtonsoft.Json.Linq;

namespace Utils.Weixin
{
    static class HttpClientExtensions
    {
        public static async Task<JToken> ReadAsWxOpenAsync(this Task<HttpResponseMessage> httpContent)
        {
            var next = await httpContent;
            return await next.ReadAsWxOpenAsync();
        }


        public static async Task<JToken> ReadAsWxOpenAsync(this HttpResponseMessage httpContent)
        {
            var jtoken = await httpContent.Content.ReadAsJTokenAsync();
            int errcode = jtoken?.Value<int>("errcode") ?? 5000;
            if (errcode != 0)
                throw Error.ThirdError(errcode.ToString(), jtoken?.Value<string>("errmsg") ?? "开放平台接口异常");
            return jtoken;
        }
    }
}
